using System;
using System.Collections.ObjectModel;
using FireStarterModeller.BO;
using Habanero.Base;
using Habanero.BO;
using NUnit.Framework;

namespace FireStarterModeller.Test.BO
{

    /// <summary>
    /// Provides a place to write custom tests for DMRelationshipPropertyProperty objects.
    /// This file is only written once and can be changed.  The Def file
    /// attached to this as a dependent is rewritten with each regeneration
    /// and contains the standard tests for DMRelationshipPropertyProperty.
    /// Regenerate this test project whenever there have been changes to the
    /// business objects.
    /// If tests are failing due to a unique setup in your application,
    /// you can either override the Create methods in TestUtils, or you
    /// can add the test to the ignore list below and reimplement it here.
    /// </summary>
    public partial class TestDMRelationshipProperty
    {
        [TestFixtureSetUp]
        public void SetupTestFixture()
        {
            //------------------------------------------------------------
            // Use this list to ignore generated tests that are failing
            // due to a unique condition in your application.
            // Remember to reimplement the test here.
            //------------------------------------------------------------
            //_ignoreList.Add("TestMethodName", "Reason for ignoring it");
            _ignoreList.Add("Test_SaveDMRelationshipProperty", "Overriden");
            base.SetupFixture();
        }

        #region Overridden Tests

        [Test]  // Ensures that a class can be successfully saved
        public void Test_SaveDMRelationshipProperty_Override()
        {
            CheckIfTestShouldBeIgnored();
            //---------------Set up test pack-------------------
            DMRelationshipProperty dMRelationshipProperty = TestUtilsDMRelationshipProperty.CreateUnsavedValidDMRelationshipProperty();

            //---------------Assert Precondition----------------
            Assert.IsTrue(dMRelationshipProperty.Status.IsNew);
            BusinessObjectCollection<DMRelationshipProperty> col = new BusinessObjectCollection<DMRelationshipProperty>();
            col.LoadAll();
            Assert.AreEqual(0, col.Count);

            //---------------Execute Test ----------------------
            dMRelationshipProperty.Save();

            //---------------Test Result -----------------------
            Assert.IsFalse(dMRelationshipProperty.Status.IsNew);
            col.LoadAll();
            Assert.AreEqual(2, col.Count);
        }

        #endregion

        [Test]
        public void Test_SaveRelProp_ShouldSave()
        {
            //---------------Set up test pack-------------------
            DMRelationshipProperty dmRelationshipProperty = TestUtilsDMRelationshipProperty.CreateUnsavedValidDMRelationshipProperty();

            //---------------Assert Precondition----------------
            Assert.IsNotNull(dmRelationshipProperty.Property);
            Assert.IsNotNull(dmRelationshipProperty.PropertyID);
            Assert.IsNotNull(dmRelationshipProperty.OwnerProperty);
            //---------------Execute Test ----------------------
            dmRelationshipProperty.Save();
            //---------------Test Result -----------------------
            Assert.IsTrue(true, "If get here all is OK");
        }

        [Test]
        public void Test_ToString()
        {
            //---------------Set up test pack-------------------
            DMRelationshipProperty dmRelationshipProperty = TestUtilsDMRelationshipProperty.CreateUnsavedValidDMRelationshipProperty();
            //---------------Assert Precondition----------------
            Assert.IsNotNull(dmRelationshipProperty.Relationship);
            Assert.IsNotNull(dmRelationshipProperty.OwnerProperty);
            //---------------Execute Test ----------------------
            string actualToString = dmRelationshipProperty.ToString();
            //---------------Test Result -----------------------
            Assert.AreEqual(dmRelationshipProperty.Relationship + " - " + dmRelationshipProperty.OwnerProperty, actualToString);
        }
        [Test]
        public void Test_ToString_RelationshipNull()
        {
            //---------------Set up test pack-------------------
            DMRelationshipProperty dmRelationshipProperty = TestUtilsDMRelationshipProperty.CreateUnsavedValidDMRelationshipProperty_WNullRelationship();
            //---------------Assert Precondition----------------
            Assert.IsNull(dmRelationshipProperty.Relationship);
            Assert.IsNotNull(dmRelationshipProperty.OwnerProperty);
            //---------------Execute Test ----------------------
            string actualToString = dmRelationshipProperty.ToString();
            //---------------Test Result -----------------------
            Assert.AreEqual(" - " + dmRelationshipProperty.OwnerProperty.ToString(), actualToString);
        } 
        [Test]
        public void Test_ToString_RelationshipNull_OwnerPropertyNull()
        {
            //---------------Set up test pack-------------------
            DMRelationshipProperty dmRelationshipProperty = TestUtilsDMRelationshipProperty.CreateUnsavedValidDMRelationshipProperty_WNullRelationship();
            dmRelationshipProperty.OwnerProperty = null;
            //---------------Assert Precondition----------------
            Assert.IsNull(dmRelationshipProperty.Relationship);
            Assert.IsNull(dmRelationshipProperty.OwnerProperty);
            //---------------Execute Test ----------------------
            string actualToString = dmRelationshipProperty.ToString();
            //---------------Test Result -----------------------
            Assert.AreEqual(dmRelationshipProperty.RelationshipPropertyID.ToString(), actualToString);
        }
        [Test]
        public void Test_ToString_RelationshipNull_OwnerPropertyNull_RelationshipPropertyIDNull()
        {
            //---------------Set up test pack-------------------
            DMRelationshipProperty dmRelationshipProperty = TestUtilsDMRelationshipProperty.CreateUnsavedValidDMRelationshipProperty_WNullRelationship();
            dmRelationshipProperty.OwnerProperty = null;
            dmRelationshipProperty.RelationshipPropertyID = null;
            //---------------Assert Precondition----------------
            Assert.IsNull(dmRelationshipProperty.Relationship);
            Assert.IsNull(dmRelationshipProperty.OwnerProperty);
            Assert.IsNull(dmRelationshipProperty.RelationshipPropertyID);
            //---------------Execute Test ----------------------
            string actualToString = dmRelationshipProperty.ToString();
            //---------------Test Result -----------------------
            Assert.AreEqual("-unnamed RelationshipProperty-", actualToString);
        }
        [Test]
        public void Test_SetOwningPropertyNull()
        {
            //---------------Set up test pack-------------------
            DMRelationshipProperty dmRelationshipProperty =
                TestUtilsDMRelationshipProperty.CreateUnsavedValidDMRelationshipProperty_WNullRelationship();
            //---------------Assert Precondition----------------
            Assert.IsNotNull(dmRelationshipProperty.OwnerProperty);
            //---------------Execute Test ----------------------
            dmRelationshipProperty.OwnerProperty = null;
            //---------------Test Result------------------------
            Assert.IsNull(dmRelationshipProperty.OwnerProperty);
        }

        [Test]
        public void Test_CannotHaveSameRelatedPropertyTwice()
        {
            //--------------- Set up test pack ------------------
            DMRelationshipProperty dmRelationshipProperty =
                TestUtilsDMRelationshipProperty.CreateSavedDMRelationshipProperty();
            IDMRelationship relationship = dmRelationshipProperty.Relationship;
            //--------------- Test Preconditions ----------------
            Assert.IsNotNull(relationship);
            //--------------- Execute Test ----------------------
            DMRelationshipProperty relationshipProperty = relationship.RelationshipProperties.CreateBusinessObject();
            relationshipProperty.RelatedProperty = dmRelationshipProperty.RelatedProperty;
            relationshipProperty.OwnerProperty = dmRelationshipProperty.OwnerProperty;
            try
            {
                relationshipProperty.Save();

                Assert.Fail("expected Err");
            }
                //---------------Test Result -----------------------
            catch (Exception ex)
            {
                StringAssert.Contains("A 'DM Relationship Property' already exists with the same identifier:", ex.Message);
            }            //--------------- Test Result -----------------------
        }

        [Test]
        public void Test_Relationship_WhenSet_ShouldSetReverseRelationshipPropRelationshipCorrectly()
        {
            //---------------Set up test pack-------------------
            DMRelationshipProperty dmRelationshipProperty = new DMRelationshipProperty();
            DMRelationship relationship = new DMRelationship();
            //---------------Assert Precondition----------------
            Assert.IsNotNull(dmRelationshipProperty.ReverseRelationshipProperty);
            Assert.IsNotNull(relationship.ReverseRelationship);
            //---------------Execute Test ----------------------
            dmRelationshipProperty.Relationship = relationship;
            //---------------Test Result -----------------------
            Assert.AreSame(relationship, dmRelationshipProperty.Relationship);
            Assert.AreSame(relationship.ReverseRelationship, dmRelationshipProperty.ReverseRelationshipProperty.Relationship);
        }

        [Test]
        public void Test_Relationship_WhenSet_UsingSetRelated_ShouldSetReverseRelationshipPropRelationshipCorrectly()
        {
            //---------------Set up test pack-------------------
            DMRelationshipProperty dmRelationshipProperty = new DMRelationshipProperty();
            DMRelationship relationship = new DMRelationship();
            ISingleRelationship singleRelationship = (ISingleRelationship)dmRelationshipProperty.Relationships["Relationship"];
            //---------------Assert Precondition----------------
            Assert.IsNotNull(dmRelationshipProperty.ReverseRelationshipProperty);
            Assert.IsNotNull(relationship.ReverseRelationship);
            //---------------Execute Test ----------------------
            singleRelationship.SetRelatedObject(relationship);
            //---------------Test Result -----------------------
            Assert.AreSame(relationship, dmRelationshipProperty.Relationship);
            Assert.AreSame(relationship.ReverseRelationship, dmRelationshipProperty.ReverseRelationshipProperty.Relationship);
        }

        [Test]
        public void Test_Relationship_WhenSet_FromReverseSide_ShouldSetReverseRelationshipPropRelationshipCorrectly()
        {
            //---------------Set up test pack-------------------
            DMRelationshipProperty dmRelationshipProperty = new DMRelationshipProperty();
            DMRelationship relationship = new DMRelationship();
            //---------------Assert Precondition----------------
            Assert.IsNotNull(dmRelationshipProperty.ReverseRelationshipProperty);
            Assert.IsNotNull(relationship.ReverseRelationship);
            //---------------Execute Test ----------------------
            dmRelationshipProperty.ReverseRelationshipProperty.Relationship = relationship.ReverseRelationship;
            //---------------Test Result -----------------------
            Assert.AreSame(relationship, dmRelationshipProperty.Relationship);
            Assert.AreSame(relationship.ReverseRelationship, dmRelationshipProperty.ReverseRelationshipProperty.Relationship);
        }

        [Test]
        public void Test_Relationship_WhenSet_FromReverseSide_UsingSetRelated_ShouldSetReverseRelationshipPropRelationshipCorrectly()
        {
            //---------------Set up test pack-------------------
            DMRelationshipProperty dmRelationshipProperty = new DMRelationshipProperty();
            DMRelationship relationship = new DMRelationship();
            ISingleRelationship singleRelationship = (ISingleRelationship)dmRelationshipProperty.ReverseRelationshipProperty.Relationships["Relationship"];
            //---------------Assert Precondition----------------
            Assert.IsNotNull(dmRelationshipProperty.ReverseRelationshipProperty);
            Assert.IsNotNull(relationship.ReverseRelationship);
            //---------------Execute Test ----------------------
            singleRelationship.SetRelatedObject(relationship.ReverseRelationship);
            //---------------Test Result -----------------------
            Assert.AreSame(relationship, dmRelationshipProperty.Relationship);
            Assert.AreSame(relationship.ReverseRelationship, dmRelationshipProperty.ReverseRelationshipProperty.Relationship);
        }

        [Test]
        public void Test_Relationship_WhenSetToNull_ShouldSetReverseRelationshipPropRelationshipCorrectly()
        {
            //---------------Set up test pack-------------------
            DMRelationshipProperty dmRelationshipProperty = new DMRelationshipProperty();
            DMRelationship relationship = new DMRelationship();
            dmRelationshipProperty.Relationship = relationship;
            //---------------Assert Precondition----------------
            Assert.IsNotNull(dmRelationshipProperty.ReverseRelationshipProperty);
            Assert.AreSame(relationship, dmRelationshipProperty.Relationship);
            Assert.AreSame(relationship.ReverseRelationship, dmRelationshipProperty.ReverseRelationshipProperty.Relationship);
            //---------------Execute Test ----------------------
            dmRelationshipProperty.Relationship = null;
            //---------------Test Result -----------------------
            Assert.IsNull(dmRelationshipProperty.Relationship);
            Assert.IsNull(dmRelationshipProperty.ReverseRelationshipProperty.Relationship);
        }

        [Test]
        public void Test_Relationship_WhenSetToNull_UsingSetRelated_ShouldSetReverseRelationshipPropRelationshipCorrectly()
        {
            //---------------Set up test pack-------------------
            DMRelationshipProperty dmRelationshipProperty = new DMRelationshipProperty();
            DMRelationship relationship = new DMRelationship();
            dmRelationshipProperty.Relationship = relationship;
            ISingleRelationship singleRelationship = (ISingleRelationship)dmRelationshipProperty.Relationships["Relationship"];
            //---------------Assert Precondition----------------
            Assert.IsNotNull(dmRelationshipProperty.ReverseRelationshipProperty);
            Assert.AreSame(relationship, dmRelationshipProperty.Relationship);
            Assert.AreSame(relationship.ReverseRelationship, dmRelationshipProperty.ReverseRelationshipProperty.Relationship);
            //---------------Execute Test ----------------------
            singleRelationship.SetRelatedObject(null);
            //---------------Test Result -----------------------
            Assert.IsNull(dmRelationshipProperty.Relationship);
            Assert.IsNull(dmRelationshipProperty.ReverseRelationshipProperty.Relationship);
        }

        [Test]
        public void Test_Relationship_WhenSetToNull_FromReverseSide_ShouldSetReverseRelationshipPropRelationshipCorrectly()
        {
            //---------------Set up test pack-------------------
            DMRelationshipProperty dmRelationshipProperty = new DMRelationshipProperty();
            DMRelationship relationship = new DMRelationship();
            dmRelationshipProperty.Relationship = relationship;
            //---------------Assert Precondition----------------
            Assert.IsNotNull(dmRelationshipProperty.ReverseRelationshipProperty);
            Assert.AreSame(relationship, dmRelationshipProperty.Relationship);
            Assert.AreSame(relationship.ReverseRelationship, dmRelationshipProperty.ReverseRelationshipProperty.Relationship);
            //---------------Execute Test ----------------------
            dmRelationshipProperty.ReverseRelationshipProperty.Relationship = null;
            //---------------Test Result -----------------------
            Assert.IsNull(dmRelationshipProperty.Relationship);
            Assert.IsNull(dmRelationshipProperty.ReverseRelationshipProperty.Relationship);
        }

        [Test]
        public void Test_Relationship_WhenSetToNull_FromReverseSide_UsingSetRelated_ShouldSetReverseRelationshipPropRelationshipCorrectly()
        {
            //---------------Set up test pack-------------------
            DMRelationshipProperty dmRelationshipProperty = new DMRelationshipProperty();
            DMRelationship relationship = new DMRelationship();
            dmRelationshipProperty.Relationship = relationship;
            ISingleRelationship singleRelationship = (ISingleRelationship)dmRelationshipProperty.ReverseRelationshipProperty.Relationships["Relationship"];
            //---------------Assert Precondition----------------
            Assert.IsNotNull(dmRelationshipProperty.ReverseRelationshipProperty);
            Assert.AreSame(relationship, dmRelationshipProperty.Relationship);
            Assert.AreSame(relationship.ReverseRelationship, dmRelationshipProperty.ReverseRelationshipProperty.Relationship);
            //---------------Execute Test ----------------------
            singleRelationship.SetRelatedObject(null);
            //---------------Test Result -----------------------
            Assert.IsNull(dmRelationshipProperty.Relationship);
            Assert.IsNull(dmRelationshipProperty.ReverseRelationshipProperty.Relationship);
        }

        [Test]
        public void Test_DMRelationshipPropertyTypesAreSame_ShouldBeOnBusinessObject()
        {
            //---------------Set up test pack-------------------
            DMRelationshipProperty dmRelationshipProperty = new DMRelationshipProperty();
            //---------------Assert Precondition----------------
            //---------------Execute Test ----------------------
            ReadOnlyCollection<IBusinessObjectRule> rules = BOHelper.GetBusinessObjectRules(dmRelationshipProperty);
            //---------------Test Result -----------------------
            TestUtilsShared.AssertHasRuleOfType<DMRelationshipPropertyTypesAreSame>(rules);
        }

        [Test]
        public void Test_OwnerClass_WhenOwnerPropNull_ShouldReturnNull()
        {
            //---------------Set up test pack-------------------
            DMRelationshipProperty dmRelationshipProperty = new DMRelationshipProperty();
            //---------------Assert Precondition----------------
            Assert.IsNull(dmRelationshipProperty.OwnerProperty);
            //---------------Execute Test ----------------------
            var ownerClass = dmRelationshipProperty.OwnerClass;
            //---------------Test Result -----------------------
            Assert.IsNull(ownerClass);
        }
        [Test]
        public void Test_OwnerClass_WhenOwnerPropClassNull_ShouldReturnNull()
        {
            DMRelationshipProperty dmRelationshipProperty = new DMRelationshipProperty();
            dmRelationshipProperty.OwnerProperty = new DMProperty();
            //---------------Assert Precondition----------------
            Assert.IsNotNull(dmRelationshipProperty.OwnerProperty);
            Assert.IsNull(dmRelationshipProperty.OwnerProperty.Class);
            //---------------Execute Test ----------------------
            var ownerClass = dmRelationshipProperty.OwnerClass;
            //---------------Test Result -----------------------
            Assert.IsNull(ownerClass);
        }
        [Test]
        public void Test_OwnerClass_ShouldReturnOwnerPropsClass()
        {
            //---------------Set up test pack-------------------
            DMRelationshipProperty dmRelationshipProperty = new DMRelationshipProperty();
            dmRelationshipProperty.OwnerProperty = new DMProperty();
            var expectedClass = new DMClass();
            dmRelationshipProperty.OwnerProperty.Class = expectedClass;
            //---------------Assert Precondition----------------
            Assert.IsNotNull(dmRelationshipProperty.OwnerProperty);
            Assert.IsNotNull(dmRelationshipProperty.OwnerProperty.Class);
            //---------------Execute Test ----------------------
            var ownerClass = dmRelationshipProperty.OwnerClass;
            //---------------Test Result -----------------------
            Assert.AreSame(expectedClass, ownerClass);
        }

        [Test]
        public void Test_IsOwnerPropCompulsory_WhenPropIsComp_ShouldRetTrue()
        {
            //---------------Set up test pack-------------------
            DMRelationshipProperty dmRelationshipProperty = new DMRelationshipProperty();
            DMProperty ownerProperty = new DMProperty();
            ownerProperty.Compulsory = true;
            dmRelationshipProperty.OwnerProperty = ownerProperty;
            //---------------Assert Precondition----------------
            Assert.IsTrue(dmRelationshipProperty.OwnerProperty.IsCompulsory);
            //---------------Execute Test ----------------------
            bool isOwnerPropComp = dmRelationshipProperty.IsOwnerPropCompulsory;
            //---------------Test Result -----------------------
            Assert.IsTrue(isOwnerPropComp);
        }

        [Test]
        public void Test_IsOwnerPropCompulsory_WhenPropNotComp_ShouldRetFalse()
        {
            //---------------Set up test pack-------------------
            DMRelationshipProperty dmRelationshipProperty = new DMRelationshipProperty();
            DMProperty ownerProperty = new DMProperty();
            ownerProperty.Compulsory = false;
            dmRelationshipProperty.OwnerProperty = ownerProperty;
            //---------------Assert Precondition----------------
            Assert.IsFalse(dmRelationshipProperty.OwnerProperty.IsCompulsory);
            //---------------Execute Test ----------------------
            bool isOwnerPropComp = dmRelationshipProperty.IsOwnerPropCompulsory;
            //---------------Test Result -----------------------
            Assert.IsFalse(isOwnerPropComp);
        }  
        
        [Test]
        public void Test_IsOwnerPropCompulsory_WhenPropNull_ShouldRetFalse()
        {
            //---------------Set up test pack-------------------
            DMRelationshipProperty dmRelationshipProperty = new DMRelationshipProperty();
            //---------------Assert Precondition----------------
            Assert.IsNull(dmRelationshipProperty.OwnerProperty);
            //---------------Execute Test ----------------------
            bool isOwnerPropComp = dmRelationshipProperty.IsOwnerPropCompulsory;
            //---------------Test Result -----------------------
            Assert.IsFalse(isOwnerPropComp);
        }
    }
}